/*
 * Copyright 2012 Zach Whaley
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.zachwhaley.android.provider;

import android.content.ContentResolver;
import android.net.Uri;
import android.provider.BaseColumns;

/**
 * Simple Lists Contract
 * @author Zach Whaley
 */
public final class ListContract
{
	/**
	 * Private constructor
	 */
	private ListContract()
	{

	}
	
	/**
	 * Special value for {@link SyncColumns#UPDATED} indicating that an entry
	 * has never been updated, or doesn't exist yet.
	 */
	public static final long UPDATED_NEVER = -2;

	/**
	 * Special value for {@link SyncColumns#UPDATED} indicating that the last
	 * update time is unknown, usually when inserted from a local file source.
	 */
	public static final long UPDATED_UNKNOWN = -1;

	/**
	 * Last time this entry was updated or synchronized
	 * @author Zach Whaley
	 */
	public interface SyncColumns
	{
		String UPDATED = "updated";
	}

	/**
	 * Interface of lists table columns
	 * @author Zach Whaley
	 */
	public interface ListsColumns extends BaseColumns
	{
		public static final String LIST_NAME = "list_name";
		public static final String LIST_CREATED = "list_created";
		public static final String LIST_EDITED = "list_edited";
	}

	/**
	 * Interface of things table columns
	 * @author Zach
	 *
	 */
	public interface ThingsColumns extends BaseColumns
	{
		public static final String LIST_ID = "list_id";
		public static final String THING_NAME = "thing_name";
		public static final String THING_CHECK = "thing_check";
		public static final String THING_CREATED = "thing_created";
		public static final String THING_EDITED = "thing_edited";
	}

	/**
	 * Content package
	 */
	public static final String CONTENT_AUTHORITY = "me.zachwhaley.android";

	/**
	 * Database base URI
	 */
	private static final Uri BASE_CONTENT_URI = Uri.parse("content://"
			+ CONTENT_AUTHORITY);

	/**
	 * URI path to lists table
	 */
	private static final String PATH_LISTS = "lists";
	/**
	 * URI path to things table
	 */
	private static final String PATH_THINGS = "things";

	/**
	 * Static class to Interface with the lists table
	 * @author Zach Whaley
	 */
	public static class Lists implements ListsColumns
	{
		// Lists content URI
		public static final Uri CONTENT_URI = BASE_CONTENT_URI.buildUpon().appendPath(PATH_LISTS).build();
		// List type
		public static final String CONTENT_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE + "/list";
		// Single list item
		public static final String CONTENT_ITEM_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE + "/list";
		// Default sort order
		public static final String DEFAULT_SORT = ListsColumns.LIST_CREATED + "ASC";

		/**
		 * Build {@link Uri} that references any list associated with the input ID
		 * @param listId List ID
		 * @return List URI 
		 */
		public static Uri buildListUri(String listId)
		{
			return CONTENT_URI.buildUpon().appendPath(listId).build();
		}

		public static Uri buildThingsDirUri(String listId)
		{
			return CONTENT_URI.buildUpon().appendPath(listId).appendPath(PATH_THINGS).build();
		}

		public static String getListId(Uri uri)
		{
			return uri.getPathSegments().get(1);
		}

		public static String generateListId(String list)
		{
			// TODO: should return something other than null
			return null;
		}
	}

	/**
	 * Static class to interface with the things table
	 */
	public static class Things implements ThingsColumns
	{
		// Things content URI
		public static final Uri CONTENT_URI = BASE_CONTENT_URI.buildUpon().appendPath(PATH_THINGS).build();
		// Thing type
		public static final String CONTENT_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE + "/thing";
		// Single thing item
		public static final String CONTENT_ITEM_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE + "/thing";
		// Default sort order
		public static final String DEFAULT_SORT = ThingsColumns.THING_CREATED + "ASC";

		/**
		 * Build {@link Uri} that references any thing associated with the input ID
		 * @param thingId Thing ID
		 * @return Thing URI
		 */
		public static Uri buildThingUri(String thingId)
		{
			return CONTENT_URI.buildUpon().appendPath(thingId).build();
		}

		/**
		 * Returns the associated ID from the URI
		 * @param uri
		 * @return Thing ID
		 */
		public static String getThingId(Uri uri)
		{
			return uri.getPathSegments().get(1);
		}

		public static String generateThingsId(String thing)
		{
			// TODO: should return something other than null
			return null;
		}
	}
	
}
